/**
 *  增加或减少(输入) 选择的商品
 */
define(function(require,exports,module) {
    // require('./plug/hammer.min');
	// require('./plug/layer/layer');
    var myPub = require('./public'); 
    $(function() {
        var oAmountBox = $('.js_amountBox');        //数量框
        var oGoodsPrice = $('.js_goodsPrice');      //产品价格
        var oSumNum = $('.js_sumNum');              //总数
        var oSumPrice = $('.js_sumPrice');                      //总价格
        var nOrderNum = $('.js_orderNum');                      //订单数量
        
        var oGoodsSizeList = $('.js_goodsSizeList');	//产品规格列表

        var minCount = $('.jsMinCount');                      //订单数量

        var memberScore =$("#costMemberScore").attr('data-memberScore');

        //最大限制和最小限制
        var maxNum = 99;
        var minNum = 1;

    	if(window.location.search != '') {
	   		var argName = window.location.search.split('&')[0].split('?')[1].split('=');
	    	
	    	if(argName[0] == 'LimitBuy' && argName[1] == 'true') {
	    		maxNum = 1;
	    	}
    	}
	    	
        
        //数量输入框
	    oSumNum.focus(function() {
	    	oGoodsSizeList.hide()
	    });
	    
	    oSumNum.blur(function() {
	    	oGoodsSizeList.show()
	    });
        

        oAmountBox.each(function(i) {
            var oHandBtn = oAmountBox.eq(i).find('.js_handBtn');
            var oGoodsPriceTxt = oGoodsPrice.eq(i).text();
            var iNow = oSumNum.eq(i).val();
            //oGoodsPrice.eq(i).html((oGoodsPriceTxt*iNow).toFixed(2));

            var minBuyCount = 1;
            if(minCount.eq(i).attr("minCount") != null && minCount.eq(i).attr("minCount") != undefined){
                minBuyCount = parseInt(minCount.eq(i).attr("minCount"));
            }
			 
            oHandBtn.hammer().on('tap',function(e) {
            	 
            	// 单价
            	var oneGoodsPrice = oGoodsPrice.eq(i).attr("pPrice");

            	// 库存
            	var kucun = $("#kucun").attr("value");
                var index = oHandBtn.index(this);
                var _iNow = oSumNum.eq(i).val();
                oSumNum.blur();
                oHandBtn.removeClass('active');
                switch(index) {
                    //减少
                    case 0:
                         if(_iNow <= minBuyCount) {
                         	oHandBtn.eq(index).addClass('active')
                        	_iNow = minBuyCount;
                            myPub.TipLayer("不能再少了");  
                         }
                         else {
                        	_iNow--;
                         }
                        break;
                    //增加
                    case 1:
                        if(_iNow > maxNum-1) {
                        	oHandBtn.eq(index).addClass('active')
                        	_iNow = maxNum; 
                            myPub.TipLayer("最多只能"+maxNum+"，不要太贪心哦");
                         }
                         else {
                        	_iNow++;
                         }
                        break;
                }
                
                if(_iNow > kucun){
                	_iNow = kucun;
                	kucun == 0 ? _iNow = 1 : _iNow = kucun
                    myPub.TipLayer("库存不足哦");
                }
                
                //检测已经在购物车
                if($(this).hasClass('incart')){
                	var cartId = $(this).attr("cartItemId");
                	updateCart(cartId,_iNow,function(){
                		iNow = _iNow;
    					oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2));
                        $("#costMemberScore").text(memberScore*iNow);
                        oSumNum.eq(i).val(iNow);
                         						myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_goodsPrice',oSumPrice);
                	},function(){
                		oSumNum.eq(i).val(iNow);
                	});
                }
                else{
                	//console.log(oneGoodsPrice)
                	iNow = _iNow;
					oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2));
                    $("#costMemberScore").text(memberScore*iNow);
                    oSumNum.eq(i).val(iNow);
                     
                }
                
                
            	
            });


            oSumNum.eq(i).on('input propertychange',function(e) { 
            	
            	var sumThis = $(this)
            	// 单价
            	var oneGoodsPrice = oGoodsPrice.eq(i).attr("pPrice");
            	// 库存
            	var kucun = $("#kucun").attr("value");
            	
            	oHandBtn.removeClass('active')
            	
                myPub.ValiNum(oSumNum.eq(i),function() {
                    myPub.TipLayer('输入正确数字');
                    oSumNum.eq(i).val(1);
                    
                    oHandBtn.eq(0).addClass('active');
                },function() {
                    var _iNow = oSumNum.eq(i).prop('value')*1;
                    // 设置最大值会导致底部上移
                    if(_iNow > maxNum-1) {
                        _iNow = maxNum;
                        oSumNum.eq(i).val(_iNow); 
                        myPub.TipLayer("最多只能"+maxNum+"，不要太贪心哦");
                        
                        oHandBtn.eq(1).addClass('active');
                     }
                    if(_iNow < minBuyCount) {
                        _iNow = minBuyCount;
                        oSumNum.eq(i).val(_iNow);
                        oHandBtn.eq(0).addClass('active');
                        myPub.TipLayer("不能再少了");
                     }
                    if(_iNow > kucun){
                    	kucun == 0 ? _iNow = 1 : _iNow = kucun
                    	oHandBtn.eq(1).addClass('active');
                        myPub.TipLayer("库存不足哦");
                    }
                    //判断物品详情页面
                    if(sumThis.hasClass('incart')){
                    	var cartId = oSumNum.eq(i).attr("cartItemId"); 
                    	updateCart(cartId,_iNow,function(){
                    		iNow = _iNow;
                            oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2));
                            $("#costMemberScore").text(memberScore*iNow);
                            myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_goodsPrice',oSumPrice);
                    	},function(){
                    		oSumNum.eq(i).val(iNow);
                    	});
                    }else{
                    	//console.log(oneGoodsPrice)
                    	oSumNum.eq(i).val(_iNow);
                    }
                });
                //$('.layermmain').css({'top':25+'%'})
                
                //详情价格动态改动
                oGoodsPrice.eq(i).html((oneGoodsPrice * oSumNum.eq(i).val()).toFixed(2));
                $("#costMemberScore").text(memberScore*iNow);
                e.preventDefault();
            });

        });
        
        oSumNum.on('focus',function() {
        	$('.layermmain').css({'top':25+'%'})
        	
        });
        oSumNum.on('blur',function() {
        	$('.layermmain').css({'top':0})
        })
       
        
        function updateCart(cartId,quantity,successfn,failfn){
        	G_JSON_POST('/shop/cartItem/updateCart',{"cartId":cartId,"quantity":quantity},function(data){
				if(data.status == "success"){
					successfn();
				}else{
					failfn();
					myPub.TipLayer(data.message);
				}
        	});
        	
        	
        	
        	//静态测试   写入ajax时 删除下面
        	/*var data = "success";
        	if(data == "success"){
				successfn();
			}else{
				failfn();
				//myPub.TipLayer(data.message);
			}*/
        };
    });

});